package com.health.security.impl;

import com.health.security.Admin;
import com.health.security.AdminService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: zipeng Li
 * 2021/6/12  21:56
 */
@Service
public class AdminDetailsService implements UserDetailsService {
    @Resource
    private AdminService adminService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        // 1.查询管理员信息
        Admin admin = adminService.getAdminByUserName(s);
        // 2.管理员不存在，抛出异常
        if(admin == null){
            throw new UsernameNotFoundException("用户名或密码错误！");
        }
        // 3.设置管理员权限
        List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList("ADMIN");
        admin.setAuthorities(authorityList);
        return admin;
    }
}
